package imlayer

import (
	"time"
	"encoding/json"
	template "wxsdk/miniapp/message/template/miniapp"
	"wxsdk/miniapp/message/template/mp"
	"wxsdk/miniapp/message/template/pub"
	"dhfshop/models/consts"
	"dhfshop/service/nsqcli"
	"github.com/astaxie/beego"
)

// 客服相关模板消息
func ToAgentTemplateMsg(AdminAccessToken,page,buyerNick,toUser,templateId,formId,msgType,msgBody string)(err error){
	var color = "#173177"
	notifyMsg := &template.TemplateMessage{}
	notifyMsg.Data.Keyword1.Color = color
	notifyMsg.Data.Keyword2.Color = color
	notifyMsg.Data.Keyword3.Color = color
	notifyMsg.Data.Keyword4.Color = color
	notifyMsg.Page = page
	notifyMsg.ToUser = toUser
	notifyMsg.TemplateId = templateId
	notifyMsg.FormId = formId
	notifyMsg.Data.Keyword1.Value = buyerNick                                   //发送人
	notifyMsg.Data.Keyword2.Value = "1"                                         //未读条数
	notifyMsg.Data.Keyword3.Value = time.Now().Format(consts.DATETIME_FORMAT) //发送时间
	if msgType == "text" {
	    body := struct {
			Content string `json:"content"` 
		}{}
		if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
		    notifyMsg.Data.Keyword4.Value = body.Content           //最新文本消息内容
		}
	} else if msgType == "image" {
	    notifyMsg.Data.Keyword4.Value = "[图片]"
	} else if msgType == "miniprogrampage" {
	    notifyMsg.Data.Keyword4.Value = "[小程序卡片]"
	} else {
	
	}
	_,_,err = template.TemplateMsgSend(AdminAccessToken, notifyMsg)
	return
}

// 客服统一消息
func UniformToAgentTemplateMsg(appId,AdminAccessToken,page,buyerNick,toUser,templateId,formId,msgType,msgBody string)(err error){
	var color = "#173177"
	uniformTempMsg := new(pub.UniformTemp)
	uniformTempMsg.ToUser = toUser
	if len(formId) > 3 {
		miniAppMsg := new(template.TemplateMessage)
		miniAppMsg.Data.Keyword1.Color = color
		miniAppMsg.Data.Keyword2.Color = color
		miniAppMsg.Data.Keyword3.Color = color
		miniAppMsg.Data.Keyword4.Color = color
		miniAppMsg.Page = page
		miniAppMsg.ToUser = toUser
		miniAppMsg.TemplateId = templateId
		miniAppMsg.FormId = formId
		miniAppMsg.Data.Keyword1.Value = buyerNick                                   //发送人
		miniAppMsg.Data.Keyword2.Value = "1"                                         //未读条数
		miniAppMsg.Data.Keyword3.Value = time.Now().Format(consts.DATETIME_FORMAT) //发送时间
		if msgType == "text" {
			body := struct {
				Content string `json:"content"` 
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				miniAppMsg.Data.Keyword4.Value = body.Content           //最新文本消息内容
			}
		} else if msgType == "image" {
			miniAppMsg.Data.Keyword4.Value = "[图片]"
		} else if msgType == "miniprogrampage" {
			miniAppMsg.Data.Keyword4.Value = "[小程序卡片]"
		} else {
		
		}
		uniformTempMsg.TemplateMessage = miniAppMsg
		uniformTempMsg.Message = nil
	} else {
		mpMsg := new(mp.Message)
		mpMsg.URL = ""
		mpMsg.ToUser = toUser
		mpMsg.TemplateID = templateId
		
		mpMsg.Data = make(map[string]*mp.DataItem)
		mpMsg.Data["Keyword1"] = new(mp.DataItem)
		mpMsg.Data["Keyword1"].Color = color
		mpMsg.Data["Keyword1"].Value = buyerNick   //发送人
		//
		mpMsg.Data["Keyword2"] = new(mp.DataItem)
		mpMsg.Data["Keyword2"].Color = color
		mpMsg.Data["Keyword2"].Value = "1"   //未读条数
		//
		mpMsg.Data["Keyword3"] = new(mp.DataItem)
		mpMsg.Data["Keyword3"].Color = color
		mpMsg.Data["Keyword3"].Value = time.Now().Format(consts.DATETIME_FORMAT) //发送时间
		//
	    mpMsg.Data["Keyword4"] = new(mp.DataItem)
		mpMsg.Data["Keyword4"].Color = color
		
		if msgType == "text" {
			body := struct {
				Content string `json:"content"` 
			}{}
			if err := json.Unmarshal([]byte(msgBody), &body); err == nil {
				mpMsg.Data["Keyword4"].Value = body.Content           //最新文本消息内容
			}
		} else if msgType == "image" {
			mpMsg.Data["Keyword4"].Value = "[图片]"
		} else if msgType == "miniprogrampage" {
			mpMsg.Data["Keyword4"].Value = "[小程序卡片]"
		} else {
		
		}
		//mpMsg.MiniProgram.AppID
		//mpMsg.MiniProgram.PagePath
		uniformTempMsg.TemplateMessage = nil
		uniformTempMsg.Message = mpMsg
	}
	if msgId, err := pub.Send(AdminAccessToken, uniformTempMsg); err != nil {
	    beego.Emergency("pub.Send:", msgId, err.Error())
	}
	return
}

// 订单确认通知
func OrderConfirmTempMsg(AdminAccessToken,page,toUser,templateId,formId,orderId,msgBody string)(err error) {
	var color = "#173177"
	notifyMsg := &template.TemplateMessage{}
	notifyMsg.Data.Keyword1.Color = color
	notifyMsg.Data.Keyword2.Color = color
	notifyMsg.Data.Keyword3.Color = color
	notifyMsg.Data.Keyword4.Color = color
	notifyMsg.Page = page
	notifyMsg.ToUser = toUser
	notifyMsg.TemplateId = templateId
	notifyMsg.FormId = formId
	notifyMsg.Data.Keyword1.Value = orderId                                      //订单号
	notifyMsg.Data.Keyword2.Value = time.Now().Format(consts.DATETIME_FORMAT)  //确认时间                                 
	notifyMsg.Data.Keyword3.Value = msgBody                                      //订单信息
	_,_,err = template.TemplateMsgSend(AdminAccessToken, notifyMsg)
	return
}

// 订单发货提醒
func OrderShipTempMsg(AdminAccessToken,page,toUser,templateId,formId,orderId,expressCorp,expressId,address string)(err error) {
	var color = "#173177"
	notifyMsg := &template.TemplateMessage{}
	notifyMsg.Data.Keyword1.Color = color
	notifyMsg.Data.Keyword2.Color = color
	notifyMsg.Data.Keyword3.Color = color
	notifyMsg.Data.Keyword4.Color = color
	notifyMsg.Page = page
	notifyMsg.ToUser = toUser
	notifyMsg.TemplateId = templateId
	notifyMsg.FormId = formId
	notifyMsg.Data.Keyword1.Value = orderId               //订单号
	notifyMsg.Data.Keyword2.Value = expressCorp           //物流公司
	notifyMsg.Data.Keyword3.Value = expressId             //物流单号
	notifyMsg.Data.Keyword4.Value = address                  //地址
	_,_,err = template.TemplateMsgSend(AdminAccessToken, notifyMsg)
	return
}

//订单评价提醒
func OrderRatedTempMsg(AdminAccessToken,page,toUser,templateId,formId,orderId,title,msgBody string)(err error) {
	var color = "#173177"
	notifyMsg := &template.TemplateMessage{}
	notifyMsg.Data.Keyword1.Color = color
	notifyMsg.Data.Keyword2.Color = color
	notifyMsg.Data.Keyword3.Color = color
	notifyMsg.Data.Keyword4.Color = color
	notifyMsg.Page = page
	notifyMsg.ToUser = toUser
	notifyMsg.TemplateId = templateId
	notifyMsg.FormId = formId
	notifyMsg.Data.Keyword1.Value = orderId                //订单编号
	notifyMsg.Data.Keyword2.Value = title                  //商品名称
	notifyMsg.Data.Keyword3.Value = msgBody                //温馨提示
	_,_,err = template.TemplateMsgSend(AdminAccessToken, notifyMsg)
	return
}

// 订单通知消息
func OrderNotifyMsg(orderId int64,status,timestamp string)(err error) {
	orderNotifyMsg := struct {
		OrderId   int64  `json:"order_id"`
		Status    string `json:"status"`
		Timestamp string `json:"time"`
	}{OrderId:orderId,Status:status,Timestamp:timestamp}
	orderNotifyJson, err := json.Marshal(&orderNotifyMsg)
	if err != nil {
		beego.Debug(err.Error())
		return
	}
	address := beego.AppConfig.String("NsqUrl")
	topic := "orderFlow"
	if err = nsqcli.StartProducer(address, topic, orderNotifyJson); err != nil {
		beego.Debug("StartProducer err:", err.Error())
	}
	return
}

// 预约成功通知
func ReserveSuccessTempMsg(AdminAccessToken,page,toUser,templateId,formId,schemaName,sourceTime string)(err error) {
	var color = "#173177"
	notifyMsg := &template.TemplateMessage{}
	notifyMsg.Data.Keyword1.Color = color
	notifyMsg.Data.Keyword2.Color = color
	notifyMsg.Data.Keyword3.Color = color
	notifyMsg.Data.Keyword4.Color = color
	notifyMsg.Page = page
	notifyMsg.ToUser = toUser
	notifyMsg.TemplateId = templateId
	notifyMsg.FormId = formId
	notifyMsg.Data.Keyword1.Value = schemaName             //预约项目
	notifyMsg.Data.Keyword2.Value = sourceTime             //体验时间
	notifyMsg.Data.Keyword3.Value = "请按时赴约"           //备注
	_,_,err = template.TemplateMsgSend(AdminAccessToken, notifyMsg)
	return
}

// 预约时间到达提醒
func ReserveArriveTempMsg(AdminAccessToken,page,toUser,templateId,formId,schemaName,sortNumber,sourceTime string)(err error) {
	var color = "#173177"
	notifyMsg := &template.TemplateMessage{}
	notifyMsg.Data.Keyword1.Color = color
	notifyMsg.Data.Keyword2.Color = color
	notifyMsg.Data.Keyword3.Color = color
	notifyMsg.Data.Keyword4.Color = color
	notifyMsg.Page = page
	notifyMsg.ToUser = toUser
	notifyMsg.TemplateId = templateId
	notifyMsg.FormId = formId
	notifyMsg.Data.Keyword1.Value = schemaName             //预约商品
	notifyMsg.Data.Keyword2.Value = sortNumber             //预约号
	notifyMsg.Data.Keyword3.Value = sourceTime             //预约时间
	notifyMsg.Data.Keyword4.Value = "请按时赴约"           //备注
	_,_,err = template.TemplateMsgSend(AdminAccessToken, notifyMsg)
	return
}

// 审核结果通知
func AuditResultTempMsg(AdminAccessToken,page,toUser,templateId,formId,name,remark,tips string)(err error) {
	var color = "#173177"
	notifyMsg := &template.TemplateMessage{}
	notifyMsg.Data.Keyword1.Color = color
	notifyMsg.Data.Keyword2.Color = color
	notifyMsg.Data.Keyword3.Color = color
	notifyMsg.Page = page
	notifyMsg.ToUser = toUser
	notifyMsg.TemplateId = templateId
	notifyMsg.FormId = formId
	notifyMsg.Data.Keyword1.Value = name       //名称
	notifyMsg.Data.Keyword2.Value = remark     //备注
	notifyMsg.Data.Keyword3.Value = tips       //温馨提示
	_,_,err = template.TemplateMsgSend(AdminAccessToken, notifyMsg)
	return
}

// 订购成功通知
func OrderSuccessTempMsg(AdminAccessToken,page,toUser,templateId,formId,content,orderTime,tips string)(err error) {
	var color = "#173177"
	notifyMsg := &template.TemplateMessage{}
	notifyMsg.Data.Keyword1.Color = color
	notifyMsg.Data.Keyword2.Color = color
	notifyMsg.Data.Keyword3.Color = color
	notifyMsg.Page = page
	notifyMsg.ToUser = toUser
	notifyMsg.TemplateId = templateId
	notifyMsg.FormId = formId
	notifyMsg.Data.Keyword1.Value = content    //订购内容
	notifyMsg.Data.Keyword2.Value = orderTime  //订购时间
	notifyMsg.Data.Keyword3.Value = tips       //温馨提示
	_,_,err = template.TemplateMsgSend(AdminAccessToken, notifyMsg)
	return
}

// 通用模板消息发送
func CommonTempMsgSend(AdminAccessToken,page,toUser,templateId,formId,value1,value2,value3,value4 string)(err error) {
	var color = "#173177"
	notifyMsg := &template.TemplateMessage{}
	notifyMsg.Data.Keyword1.Color = color
	notifyMsg.Data.Keyword2.Color = color
	notifyMsg.Data.Keyword3.Color = color
	notifyMsg.Data.Keyword4.Color = color
	notifyMsg.Page = page
	notifyMsg.ToUser = toUser
	notifyMsg.TemplateId = templateId
	notifyMsg.FormId = formId
	notifyMsg.Data.Keyword1.Value = value1             
	notifyMsg.Data.Keyword2.Value = value2             
	notifyMsg.Data.Keyword3.Value = value3             
	notifyMsg.Data.Keyword4.Value = value4
	_,_,err = template.TemplateMsgSend(AdminAccessToken, notifyMsg)
	return
}

